package com.atguigu.meinian.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.meinian.mapper.MemberMapper;
import com.atguigu.meinian.mapper.OrderMapper;
import com.atguigu.meinian.mapper.SetmealMapper;
import com.atguigu.meinian.pojo.Order;
import com.atguigu.meinian.pojo.Setmeal;
import com.atguigu.meinian.service.MemberService;
import com.atguigu.meinian.service.ReportService;
import com.atguigu.meinian.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import sun.dc.pr.PRError;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zbystart
 * @create 2021-06-28 18:08
 */
@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private MemberMapper memberMapper;

    /**
     *                 todayNewMember :0, // 新增会员数
     *
     *                 totalMember :0,// 总会员数
     *                 thisWeekNewMember :0,// 本周新增会员数
     *                 thisMonthNewMember :0,// 本月新增会员数
     *
     *                 todayOrderNumber :0,// 今日预约数
     *                 todayVisitsNumber :0,// 今日出游数
     *                 thisWeekOrderNumber :0,// 本周预约数
     *                 thisWeekVisitsNumber :0,// 本周出游数
     *
     *                 thisMonthOrderNumber :0,// 本月预约数
     *                 thisMonthVisitsNumber :0,// 本月出游数
     *                 hotSetmeal :[
     *                     {name:'海南7天6晚游套餐',setmeal_count:200,proportion:0.222},
     *                     {name:'深圳3天2晚游套餐',setmeal_count:200,proportion:0.222}
     *                 ]
     * @return
     */
    @Override
    public Map<String, Object> getBusinessReportData() {
        try {
            HashMap<String, Object> map = new HashMap<>();

            // 获取当前统计的日期
            Date date = new Date();
            // 当前日期所在的周的星期一的日期
            String firstDayOfWeek = DateUtils.parseDate2String(DateUtils.getFirstDayOfWeek(date));
            // 当前日期的1号
            String firstDayOfMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday(DateUtils.getFirstDay4ThisMonth()));
            // 当前日期
            String toDay =  DateUtils.parseDate2String(date);

            // 会员统计
            Integer count = memberMapper.selectMemberRegisterCount();
            map.put("totalMember",count);

            count = memberMapper.selectMemberRegisterCountByToDay(toDay);
            map.put("todayNewMember",count);
            count = memberMapper.selectMemberRegisterCountByThisWeekNewMember(firstDayOfMonday);
            map.put("thisWeekNewMember",count);
            count = memberMapper.selectMemberRegisterCountByThisWeekNewMember(firstDayOfWeek);
            map.put("thisMonthNewMember",count);

            count = orderMapper.selectOrderCountByDate(toDay);
            map.put("todayOrderNumber",count);
            count = orderMapper.selectTodayOrderCountByDateAndStatus(toDay,0);
            map.put("todayVisitsNumber",count);
            count = orderMapper.selectOrderCountByFirstDayOfMonday(firstDayOfMonday);
            map.put("thisWeekOrderNumber",count);
            count = orderMapper.selectOrderCountByFirstDayOfMondayAndStatus(firstDayOfMonday,1);
            map.put("thisWeekVisitsNumber",count);

            count = orderMapper.selectOrderCountByFirstDayDayOfMonday(firstDayOfMonday);
            map.put("thisMonthOrderNumber",count);
            count = orderMapper.selectOrderCountByFirstDayOfMondayAndStatus(firstDayOfMonday,1);
            map.put("thisMonthVisitsNumber",count);

            List<HashMap<String, Object>> hashMaps = orderMapper.selectHotSetmealOrderCountAndScale(6);
            hashMaps = hashMaps.stream().map(item -> {
                Setmeal setmeal = setmealMapper.selectSetmealById((Integer) item.get("name"));
                item.put("name",setmeal.getName());
                return item;
            }).collect(Collectors.toList());

            map.put("hotSetmeal",hashMaps);
            map.put("reportDate",toDay);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
